//
//  ViewControllerForDestination.m
//  ProjectA
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewControllerForDestination.h"
#import "ViewControllerForDestinationPhoto.h"

#import "ViewForDestinationHeader.h"    //V
#import "CellForDestination.h"
#import "ModelForDestination.h" //M




@interface ViewControllerForDestination ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *arrForImages;
@property (nonatomic, strong) LWHImageViewForDestination *imageView;
@property (nonatomic, strong) NSMutableArray *arrForData;
@property (nonatomic, assign) CGFloat marginTop;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewControllerForDestination
- (void)dealloc {

    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_arrForImages release];
    [_imageView release];
    [_arrForData release];
  
    [_collectionView release];
    
    [_titleForBar release];
    [_url release];
    [super dealloc];
    
}
- (void)setNightDayModel {
    [super setNightDayModel];
    
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
}
- (void)setLightDayModel {
    [super setLightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
}
- (void)viewWillAppear:(BOOL)animated {
    
    if (ISNIGHT) {
        [self setNightDayModel];
        
    } else {
        [self setLightDayModel];
        
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 加载数据菊花
   // [self.view addSubview:[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame]];
    
    [self handleData];
    
    [self settingsNavigationController];
    
    [self createCollectionView];

}

- (void)settingsNavigationController {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];
    
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 200) collectionViewLayout:flowLayout];
    [flowLayout release];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(-200, 0, 0, 0);
    self.collectionView.alwaysBounceVertical = YES;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CellForDestination class] forCellWithReuseIdentifier:@"pool"];
    [self.collectionView registerClass:[ViewForDestinationHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForHeader"];
    
    NSLog(@"%@", self.url);

}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForImages.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    
        return CGSizeMake(0, self.view.frame.size.height - 200);
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    ViewForDestinationHeader *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForHeader" forIndexPath:indexPath];
    
    if (self.arrForData.count) {
        
        ModelForDestination *model = [self.arrForData lastObject];
        
       // [view.imageView sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        [view.imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"iconfont-zhanweitu"]];
        view.labelForName.text = model.name;
        NSString *count = [NSString stringWithFormat:@"%@ 去过 / %@ 喜欢", model.visited_count, model.wish_to_go_count];
        view.labelForCount.text = count;
        return view;
    }
    return view;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CellForDestination *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    if (self.arrForImages.count) {
        [cell passData:[self.arrForImages objectAtIndex:indexPath.item] item:indexPath.item];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((self.view.bounds.size.width - 40) / 4.f, (self.view.bounds.size.width - 40) / 4.f);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.y < 49) {
        scrollView.contentOffset = CGPointMake(0, 49.f);
    }
    if (self.marginTop != scrollView.contentInset.top) {
        self.marginTop = scrollView.contentInset.top;
    }
    
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat newoffsetY = offsetY + self.marginTop;
    
    if (newoffsetY >= 0 && newoffsetY <= 264) {
        
        self.barBackgroundView.alpha = newoffsetY / 264.f;
    
    } else if (newoffsetY > 264){
        self.barBackgroundView.alpha = 1;
    } else {
        self.barBackgroundView.alpha = 0;
    }
    
    if (newoffsetY > 264) {
        self.navigationItem.title = self.titleForBar;
    } else {
        self.navigationItem.title = @"";
    }
    
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    //http://api.breadtrip.com//destination/place/3/9990/photos/?gallery_mode=1&count=18&start=0
    if (scrollView.contentOffset.y < 50) {
        NSMutableString *url = [self.url stringByAppendingString:@"photos/?gallery_mode=1&count=18&start=0"].mutableCopy;
        ViewControllerForDestinationPhoto *photo = [[ViewControllerForDestinationPhoto alloc] init];
        NSLog(@"%@", url);
        photo.url = url;
        photo.titleForBar = self.titleForBar;
        self.tabBarController.tabBar.hidden = YES;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:photo animated:YES];
        [photo release];
        self.hidesBottomBarWhenPushed = NO;
    }
    
}


- (void)houtuiAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)handleData {
    self.arrForData = [NSMutableArray array];
    self.arrForImages = @[@"iconfont-xiaolian(huise)", @"iconfont-shijieditu (huise)", @"iconfont-jiangbei(huise)", @"iconfont-wujiaoxing(huise)", @"iconfont-biaoqian(huise)"].mutableCopy;
    [NetWorkHandler networkingWithUrl:self.url comleteHandle:^(id result, NSData *data) {
       
        ModelForDestination *model = [[ModelForDestination alloc] init];
        [model setValuesForKeysWithDictionary:result];
        NSArray *arr = [result objectForKey:@"hottest_places"];
        NSDictionary *dic = [arr firstObject];
        [model setValuesForKeysWithDictionary:dic];
        [self.arrForData addObject:model];
        [model release];

        [self.collectionView reloadData];
      //  [[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame] didFinishLoad];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
